/**
 * Created by tot on 16-11-26.
 */
// 评论对象
function Comment(id, text, commentDesc, relatedComment,author) {
    this.id = id;
    this.text = text;
    this.commentDesc = commentDesc;
    this.relatedComment = relatedComment;
    this.author = author;

    this.toHtmlDom = function () {

        var rootDiv = document.createElement("div");
        var userDiv = createChildNode(rootDiv,"div",
            {"class":"col-md-1 col-sm-1  hidden-xs"});
        var commentDiv = createChildNode(rootDiv,"div");
        createChildNode(rootDiv,"hr",{"class":"col-md-12 col-sm-12"});


        // 品论者头像
        var authorImg = createChildNode(userDiv,"img",
            {"src":this.author.imageUrl,"class":"jf-user-image"});

        // 评论内容

        commentDiv.setAttribute("class","col-md-11 col-sm-11");
        commentDiv.setAttribute("style","padding: 0px");

        var firstComment = document.createElement("div");
        commentDiv.appendChild(firstComment);
        firstComment.setAttribute("class","col-md-12");
        firstComment.setAttribute("style","padding: 0px");
        var firstAuthorA = document.createElement("a");
        firstComment.appendChild(firstAuthorA);
        firstAuthorA.setAttribute("href",this.author.homeUrl);
        firstAuthorA.appendChild(document.createTextNode(this.author.name));
        var firstCommentTxt = document.createTextNode(":"+this.text);
        firstComment.appendChild(firstCommentTxt);

        //添加关联评论
        if(this.relatedComment){
            var secondComment = document.createElement("div");
            commentDiv.appendChild(secondComment);
            secondComment.setAttribute("class","col-md-12");
            secondComment.setAttribute("style","background-color: #dcdee8;padding: 10px 0px 10px 20px;margin-top: 10px");

            var secondCommentSpan = document.createElement("span");
            secondComment.appendChild(secondCommentSpan);
            secondCommentSpan.setAttribute("style","position: absolute; top:-9px;left:20px");
            var secondCommentSpanI = document.createElement("i");
            secondCommentSpan.appendChild(secondCommentSpanI);
            secondCommentSpanI.setAttribute("style","font-style:normal;color: #dcdee8");
            secondCommentSpanI.appendChild(document.createTextNode("◆"));

            var secondCommentAuthorA = document.createElement("a");
            secondComment.appendChild(secondCommentAuthorA);
            secondCommentAuthorA.setAttribute("href",this.relatedComment.author.homeUrl);
            secondCommentAuthorA.appendChild(document.createTextNode(this.relatedComment.author.name));

            secondComment.appendChild(document.createTextNode(":"+this.relatedComment.text));
        }

        // 描述信息

        var commentDescDiv = document.createElement("div");
        commentDiv.appendChild(commentDescDiv);
        commentDescDiv.setAttribute("class","col-md-12");
        commentDescDiv.setAttribute("style","margin: 10px 0px;padding: 0px;font-size: 13px;color: #c0c2cc");

        //评论创建日期
        var commentDescDate = document.createElement("div");
        commentDescDiv.appendChild(commentDescDate);
        commentDescDate.setAttribute("class","col-md-3 col-sm-3 col-xs-4");
        commentDescDate.setAttribute("style","padding: 0px");
        commentDescDate.appendChild(document.createTextNode(this.commentDesc.createDateStr));

        //评论操作
        var commentOpDiv = document.createElement("div");

        commentDescDiv.appendChild(commentOpDiv);
        commentOpDiv.setAttribute("class","col-md-3 col-sm-3 col-xs-4 jf-float-right");
        commentOpDiv.setAttribute("style","float: right");

        var commentTopA = document.createElement("a");
        commentOpDiv.appendChild(commentTopA);

        commentTopA.setAttribute("href","javascript:0;")
        commentTopA.setAttribute("class","jf-float-right")

        var topSpan = document.createElement("span");
        commentTopA.appendChild(topSpan);
        topSpan.setAttribute("class","glyphicon glyphicon-thumbs-up")
        topSpan.setAttribute("style","font-size: 14px")

        var topNumSpan = createChildNode(commentTopA,"span");
        topNumSpan.setAttribute("style","font-size: 13px;color: black");
        topNumSpan.appendChild(document.createTextNode("("+this.commentDesc.topNum+")"))

        var sepSpan = createChildNode(commentOpDiv,"span",{
            "style":"margin: 0px 3px",
            "class":"jf-float-right"
        });
        sepSpan.appendChild(document.createTextNode("|"));

        var replyA = createChildNode(commentOpDiv,"a");
        replyA.setAttribute("href","javascript:0;");
        replyA.setAttribute("class","jf-float-right");
        //绑定单击事件

        replyA.onclick = function (e) {
            alert("aaaaa");
        };
        replyA.appendChild(document.createTextNode("回复"));

        return rootDiv;
    }

}

function createChildNode(parentNode,childNodeName,attrObj) {
    var childNode = document.createElement(childNodeName);
    parentNode.appendChild(childNode)
    for(key in attrObj){
        childNode.setAttribute(key,attrObj[key]);
    }
    return childNode;
}

//评论描述对象
function CommentDesc(topNum, createDateStr) {
    this.topNum = topNum;
    this.createDateStr = createDateStr;
}

function SimpleUser(id, name, homeUrl, imageUrl) {
    this.id = id;
    this.name = name;
    this.homeUrl = homeUrl;
    this.imageUrl = imageUrl;
}


// function initCommentBox(selector, user, placeholder, buttonName) {
function initCommentBox(param) {

    var elementId = param.elementId;

    if(elementId == undefined || elementId == null || elementId == ""){
        console.error("用于存放comment box的dom的id不能为空");
        return;
    }

    var user = (param.user == undefined || param.user == null) ? {id:"-1",name:"游客"}:param.user;
    var placeHolder = (param.placeHolder == undefined || param.placeHolder == null) ? "评论" : param.placeHolder;
    var buttonName = (param.buttonName == undefined || param.buttonName == null) ? "提交" : param.buttonName;
    var box = document.getElementById(elementId);
    if(box == null || box == undefined){
        console.error("不能找到id="+elementId+"的dom");
        return;
    }


    //用户信息div
    var userDiv = document.createElement("div");
    userDiv.setAttribute("class", "col-md-1 col-sm-1 hidden-xs");
    var userImg = document.createElement("img");
    userImg.setAttribute("src", user.imageUrl);
    userImg.setAttribute("class", "jf-user-image");
    userDiv.appendChild(userImg);

    box.appendChild(userDiv);

    var commentBoxDiv = document.createElement("div");

    box.appendChild(commentBoxDiv);

    commentBoxDiv.setAttribute("class", "col-md-11 col-sm-11");
    commentBoxDiv.setAttribute("style", "padding: 0 0px");

    var commentBoxTextArea = document.createElement("textarea");
    commentBoxDiv.appendChild(commentBoxTextArea);
    commentBoxTextArea.setAttribute("class", "jf-comment-box");
    commentBoxTextArea.setAttribute("placeholder", placeHolder);

    var commentOPDiv = document.createElement("div");
    commentBoxDiv.appendChild(commentOPDiv);

    var emojiA = document.createElement("a");
    commentOPDiv.appendChild(emojiA);
    emojiA.setAttribute("class", "dropdown");
    emojiA.setAttribute("title", "添加emoji");
    emojiA.setAttribute("href", "javascript:0;");

    var emojiIcon = document.createElement("img");
    emojiIcon.setAttribute("style", "width:23px;height: 23px");
    emojiIcon.setAttribute("src", "");
    emojiA.appendChild(emojiIcon);

    var emojiPanel = document.createElement("div");
    emojiA.appendChild(emojiPanel);
    emojiPanel.setAttribute("class", "dropdown-menu emoji-panel");
    emojiPanel.setAttribute("style", "position: absolute;top: 24px;background-color:white;border: none");

    var psPan = document.createElement("span");
    emojiPanel.appendChild(psPan);


    psPan.setAttribute("style", "position: absolute; top:-10px;left:4px");
    var spanI = document.createElement("i");
    psPan.appendChild(spanI);
    spanI.setAttribute("style", "font-style:normal;color: white");
    var txt = document.createTextNode("◆");
    spanI.appendChild(txt);

    // emoji div

    var emojiArray = [
        "smile",
        "laughing",
        "blush",
        "smiley",
        "relaxed",
        "smirk",
        "heart_eyes",
        "kissing_heart",
        "kissing_closed_eyes",
        "flushed",
        "relieved",
        "satisfied",
        "grin",
        "wink",
        "stuck_out_tongue_winking_eye",
        "stuck_out_tongue_closed_eyes",
        "grinning",
        "kissing",
        "kissing_smiling_eyes",
        "stuck_out_tongue",
        "sleeping",
        "worried",
        "frowning",
        "anguished",
        "open_mouth",
        "grimacing",
        "confused",
        "hushed",
        "expressionless",
        "unamused",
        "sweat_smile",
        "sweat",
        "disappointed_relieved",
        "weary",
        "pensive",
        "disappointed",
        "confounded",
        "fearful",
        "cold_sweat",
        "persevere",
        "cry",
        "sob",
        "joy",
        "astonished",
        "scream",
        "neckbeard",
        "tired_face",
        "angry",
        "rage",
        "triumph",
        "sleepy",
        "yum",
        "mask",
        "sunglasses",
        "dizzy_face",
        "neutral_face",
        "no_mouth",
        "innocent",
        "alien",
        "heart"
    ];

    for (index in emojiArray){
        var emojiBlock = document.createElement("div");
        emojiBlock.setAttribute("class","emoji-block");
        emojiBlock.setAttribute("title",emojiArray[index]);
        emojiBlock.setAttribute("data-emoji",emojiArray[index]);
        emojiPanel.appendChild(emojiBlock);
    }


    //代码格式化按钮
    var codeA = document.createElement("a");
    codeA.setAttribute("href","javascript:0;");
    var codeIcon = document.createElement("img");
    codeA.appendChild(codeIcon);
    codeIcon.setAttribute("style","margin-left:10px;width:23px;height: 23px");
    codeIcon.setAttribute("title","添加代码片段");
    codeIcon.setAttribute("src","");

    commentOPDiv.appendChild(codeA);

    var submitButton = document.createElement("button");
    commentOPDiv.appendChild(submitButton);
    submitButton.setAttribute("style","float: right");
    var submitTxt = document.createTextNode(buttonName);
    submitButton.appendChild(submitTxt);
}


//点击搜索到的tag时，将被点击的tag放到页面上去
//动态绑定事件
$("#wow").on("click",".jf-comment-reply",function (event) {
    alert("aaa");
    // var a = $(this).find("a");
    // var tagId = a.attr("data-id");
    // var tagName = a[0].innerHTML;
    // var tag = {name:tagName,id:tagId};
    // addOneTagInJFTagBox(tag);
    // event.stopPropagation();

});


function showReplyCommentBox() {
    // jf-comment-reply
}

